﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using Kitware.VTK;


namespace HAVTKPack
{

    public class ThresholdGeometryFilter : GeometryFilter
    {

        #region 构造函数  


        public ThresholdGeometryFilter(List<int[,]> effectiveSign):base("threshold")
        {

            this.EffectiveSign = effectiveSign;

        }


        public ThresholdGeometryFilter(double[, ,] effectiveSign)
            : base("threshold")
        {

            //this.EffectiveSign = effectiveSign;

        }

        public ThresholdGeometryFilter()
            : base("threshold")
        {

            //int rows = ((HydrolicGridGeometry)(((GeometryNode)this.ParentNode .Scene .GetNode("grid")).Geometry)).Hg.NRows;
            //int cols = ((HydrolicGridGeometry)(((GeometryNode)this.ParentNode.Scene.GetNode("grid")).Geometry)).Hg.NCols;
            //int layers = ((HydrolicGridGeometry)(((GeometryNode)this.ParentNode.Scene.GetNode("grid")).Geometry)).Hg.NLayers;
        }

        #endregion

 

        List<int [,]> EffectiveSign;
        /// <summary>
        /// 单元的有效性标记数组
        /// </summary>
        public List<int[,]> EffectiveSign1
        {
            get { return EffectiveSign; }
            set {
                //通过该类的实例A.EffectiveSign1=B时 才会触发  
                //构造函数赋值不会触发
                bool isChanged = false;
                isChanged = IsEffectiveScalarSignChanged.IsCellSignChanged(this.EffectiveSign, value);

                if (isChanged == true)
                {
                    EffectiveSign = value;

                    this.OnCellEffectiveSignChanged();
                }

            }
        }



        #region    VTK流水线所需对象
     
         [NonSerialized]
        vtkThreshold threshold;
         [NonSerialized]
         vtkPolyData data;
       
         vtkDataSetMapper clipgridMapper;
         vtkActor wireActor;

         [NonSerialized]
        vtkConnectivityFilter con;
         [NonSerialized]
        vtkGeometryFilter geo;

        #endregion

       


        public override void Render()
        {
            try
            {

                if (this.EffectiveSign == null)
                {
                    return;
                }

                //vtkPolyData data = this.ParentNode.TransFormPolyDataFilter.GetOutput();
                data = this.InputPolyData;
                data.Update();


                vtkFloatArray scalars = vtkFloatArray.New();
                this.SetCellScalarsValue(scalars);
             

                data.GetCellData().SetScalars(scalars);

                threshold = vtkThreshold.New();
                threshold.SetInput(data);

                //设置对标量属性值的第几个分量值进行操作，默认为第0个
                threshold.ThresholdByUpper(1);


                //将 threshold .GetOutput () 输出的 vtkUnstructuredGrid 类型转换成 vtkPolyData
                con = vtkConnectivityFilter.New();
                con.SetInput(threshold.GetOutput());
                geo = vtkGeometryFilter.New();
                geo.SetInput(con.GetOutput());
                //保存vtkPolyData
                //this.OutputPolyData = data;
                this.OutputPolyData = geo.GetOutput();


                clipgridMapper = vtkDataSetMapper.New();
                clipgridMapper.SetInputConnection(threshold.GetOutputPort());
                clipgridMapper.SetScalarModeToUseCellData();
                wireActor = vtkActor.New();
                wireActor.SetMapper(clipgridMapper);
                wireActor.GetProperty().SetRepresentationToWireframe();
                wireActor.GetProperty().SetColor(1, 0, 0);


                //this.ParentNode.Scene.VtkControl.RenderWindow.GetRenderers().GetFirstRenderer().AddActor(wireActor);


            }
            catch (Exception)
            {
                System.Windows.Forms.MessageBox.Show("ThresholdGeometryFilter渲染过程出错！");
                return;
            }

         
        }


        public override void StopRender()
        {
            //throw new NotImplementedException();
        }


        #region 属性改变

        private void OnCellEffectiveSignChanged()
        {
            if (this.EffectiveSign == null)
            {
                return;
            }

            this.GetNewCellScalars();

        }

        #endregion

        /// <summary>
        /// </summary>
        /// 属性 EffectiveSign1 改变后执行
        private void GetNewCellScalars()
        {
            vtkFloatArray scalars = vtkFloatArray.New();
            this.SetCellScalarsValue(scalars);
            data.GetCellData().SetScalars(scalars);
            //threshold.SetInput(data);
            threshold.Update();


          
        }



        /// <summary>
        /// 设置Scalars
        /// </summary>
        /// <param name="scalars"></param>
        protected void SetCellScalarsValue(vtkFloatArray scalars)
        {

            if (EffectiveSign == null)
            {
                return;
            }
   
            for (int layer = 0; layer < EffectiveSign.Count; layer++)
            {
                for (int rows = 0; rows < EffectiveSign[layer].GetLength(0); rows++)
                {
                    for (int cols = 0; cols < EffectiveSign[layer].GetLength(1); cols++)
                    {
                        #region 外代码 为方便测试
                        //伪代码
                        //if (layer <= 1)
                        //{
                        //    if (rows <= 1 && cols <= 1)
                        //    {
                        //        scalars.InsertNextTuple1(0);
                        //    }
                        //    else
                        //    {
                        //        scalars.InsertNextTuple1(1);
                        //    }
                        //}
                        //else
                        //{
                        //    scalars.InsertNextTuple1(1);
                        //}
                        #endregion

                        scalars.InsertNextTuple1(EffectiveSign[layer][rows, cols]); 

                    }
                }
            }

        }







    }


}
